/*
 * Copyright (c) 2021-2021. Gardel
 *
 * This file is part of Source2Pdf (https://gitee.com/gardel/source2pdf).
 *
 * Source2Pdf is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * Source2Pdf is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with Source2Pdf.  If not, see <http://www.gnu.org/licenses/>.
 */

package cn.sunxinao.source2pdf;

import cn.sunxinao.source2pdf.controller.MainController;
import cn.sunxinao.source2pdf.utils.ResizeHelper;
import cn.sunxinao.source2pdf.utils.TaskUtils;
import java.io.IOException;
import java.net.URL;
import javafx.application.Application;
import javafx.fxml.FXMLLoader;
import javafx.geometry.Rectangle2D;
import javafx.scene.Scene;
import javafx.scene.image.Image;
import javafx.scene.paint.Color;
import javafx.stage.Screen;
import javafx.stage.Stage;
import javafx.stage.StageStyle;

public class MainApp extends Application {
    @Override
    public void start(Stage primaryStage) throws IOException {
        Class<MainApp> mainAppClass = MainApp.class;
        FXMLLoader fxmlLoader = new FXMLLoader(mainAppClass.getResource("main-view.fxml"));
        fxmlLoader.setControllerFactory(controller -> new MainController(primaryStage));
        double width = 900;
        double height = 300;
        try {
            Rectangle2D bounds = Screen.getScreens().get(0).getBounds();
            width = bounds.getWidth() / 1.25;
            height = bounds.getHeight() / 1.25;
        } catch (Exception ignored) {
        }
        primaryStage.setTitle("代码打印");
        Scene scene = new Scene(fxmlLoader.load(), width, height);
        primaryStage.setScene(scene);
        URL logoResource = mainAppClass.getResource("/img/logo.png");
        if (logoResource != null) {
            primaryStage.getIcons().add(new Image(logoResource.toString()));
        }
        primaryStage.initStyle(StageStyle.TRANSPARENT);
        scene.setFill(Color.TRANSPARENT);
        ResizeHelper.addResizeListener(primaryStage, 600, 300, 1280, 720);
        primaryStage.show();
        //ScenicView.show(scene); // for debug
    }

    public static void main(String[] args) {
        launch(args);
        try {
            if (!TaskUtils.shutdown()) {
                System.err.println("Can not stop the thread pool.");
            }
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
